namespace Study1.Common;

/// <summary>
/// 统一API返回结果类
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class ApiResult<T>
{
    /// <summary>
    /// 状态码
    /// </summary>
    public int Code { get; set; }
    
    /// <summary>
    /// 返回消息
    /// </summary>
    public string Message { get; set; } = string.Empty;
    
    /// <summary>
    /// 返回数据
    /// </summary>
    public T? Data { get; set; }
    
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success => Code == (int)ResultCode.Success;
    
    /// <summary>
    /// 时间戳
    /// </summary>
    public long Timestamp { get; set; } = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

    /// <summary>
    /// 构造函数
    /// </summary>
    public ApiResult() { }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="code">状态码</param>
    /// <param name="message">消息</param>
    /// <param name="data">数据</param>
    public ApiResult(int code, string message, T? data = default)
    {
        Code = code;
        Message = message;
        Data = data;
    }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="resultCode">结果码枚举</param>
    /// <param name="message">消息</param>
    /// <param name="data">数据</param>
    public ApiResult(ResultCode resultCode, string message, T? data = default)
    {
        Code = (int)resultCode;
        Message = message;
        Data = data;
    }
}

/// <summary>
/// 无数据的统一API返回结果类
/// </summary>
public class ApiResult : ApiResult<object>
{
    /// <summary>
    /// 构造函数
    /// </summary>
    public ApiResult() : base() { }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="code">状态码</param>
    /// <param name="message">消息</param>
    public ApiResult(int code, string message) : base(code, message) { }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="resultCode">结果码枚举</param>
    /// <param name="message">消息</param>
    public ApiResult(ResultCode resultCode, string message) : base(resultCode, message) { }
}

/// <summary>
/// API结果静态工厂类
/// </summary>
public static class ApiResultFactory
{
    /// <summary>
    /// 成功返回（无数据）
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns></returns>
    public static ApiResult Success(string message = "操作成功")
    {
        return new ApiResult(ResultCode.Success, message);
    }

    /// <summary>
    /// 成功返回（有数据）
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    /// <param name="data">数据</param>
    /// <param name="message">消息</param>
    /// <returns></returns>
    public static ApiResult<T> Success<T>(T data, string message = "操作成功")
    {
        return new ApiResult<T>(ResultCode.Success, message, data);
    }

    /// <summary>
    /// 失败返回
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <param name="code">错误码</param>
    /// <returns></returns>
    public static ApiResult Fail(string message, ResultCode code = ResultCode.BusinessError)
    {
        return new ApiResult(code, message);
    }

    /// <summary>
    /// 失败返回（有数据）
    /// </summary>
    /// <typeparam name="T">数据类型</typeparam>
    /// <param name="message">错误消息</param>
    /// <param name="data">数据</param>
    /// <param name="code">错误码</param>
    /// <returns></returns>
    public static ApiResult<T> Fail<T>(string message, T? data = default, ResultCode code = ResultCode.BusinessError)
    {
        return new ApiResult<T>(code, message, data);
    }

    /// <summary>
    /// 参数错误
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <returns></returns>
    public static ApiResult BadRequest(string message = "请求参数错误")
    {
        return new ApiResult(ResultCode.BadRequest, message);
    }

    /// <summary>
    /// 未授权
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <returns></returns>
    public static ApiResult Unauthorized(string message = "未授权访问")
    {
        return new ApiResult(ResultCode.Unauthorized, message);
    }

    /// <summary>
    /// 资源不存在
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <returns></returns>
    public static ApiResult NotFound(string message = "资源不存在")
    {
        return new ApiResult(ResultCode.NotFound, message);
    }

    /// <summary>
    /// 服务器内部错误
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <returns></returns>
    public static ApiResult InternalServerError(string message = "服务器内部错误")
    {
        return new ApiResult(ResultCode.InternalServerError, message);
    }

    /// <summary>
    /// 数据验证失败
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <returns></returns>
    public static ApiResult ValidationError(string message = "数据验证失败")
    {
        return new ApiResult(ResultCode.ValidationError, message);
    }

    /// <summary>
    /// 数据已存在
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <returns></returns>
    public static ApiResult DataExists(string message = "数据已存在")
    {
        return new ApiResult(ResultCode.DataExists, message);
    }

    /// <summary>
    /// 数据不存在
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <returns></returns>
    public static ApiResult DataNotExists(string message = "数据不存在")
    {
        return new ApiResult(ResultCode.DataNotExists, message);
    }
}